package br.com.fogas.medicao.adapter;

import java.util.List;

import br.com.fogas.database.entidade.PlanoVisitas;
import br.com.fogas.database.enums.PlanoVisitaStatusEnum;
import br.com.fogas.medicao.R;
import br.com.fogas.medicao.utils.MedicaoUtils;
import butterknife.ButterKnife;
import butterknife.InjectView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class PlanoClientesAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	
	private List<PlanoVisitas> lista;
	
	public PlanoClientesAdapter(Context context, List<PlanoVisitas> planoVisitas) {
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
		this.lista = planoVisitas;
	}
	
	@Override
	public int getCount() {
		return lista.size();
	}

	@Override
	public Object getItem(int position) {
		return lista.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		ViewHolder holder = null;
		
		if(view == null) {
			view = mInflater.inflate(R.layout.plano_clientes_list_view, null);
			holder = new ViewHolder(view);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		} 
	
		PlanoVisitas planoVisitas = (PlanoVisitas) getItem(position);
		
		updateViews(view, holder, planoVisitas);
		
		return view;
	}
	
	private void updateViews(View view, ViewHolder holder, final PlanoVisitas plano) {
		holder.nome.setText(MedicaoUtils.nomeCliente(plano.getCliente()));
		
		if(plano.getStatusEnum().equals(PlanoVisitaStatusEnum.FINALIZADO)) {
			holder.status.setTextAppearance(view.getContext(), R.style.lista_plano_item_status_verde_label);
		} else {
			holder.status.setTextAppearance(view.getContext(), R.style.lista_plano_item_status_vermelho_label);
		}
		holder.status.setText(plano.getStatusEnum().getDescricao());
	}
	
	static class ViewHolder {
		@InjectView(R.id.tvListaNomeClientes) TextView nome;
		@InjectView(R.id.tvListaStatusClientes) TextView status;
		
		public ViewHolder(View view) {
			ButterKnife.inject(this, view);
		}
	}
}